package com.dongdongshop.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.service.seller.SellerService;
import com.dongdongshop.service.seller_jpa.SellerJpaService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class UserRealm extends AuthorizingRealm {
    @Reference
    private SellerService sellerService;
    @Reference
    private SellerJpaService sellerJpaService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
/*        TbSeller seller = sellerService.getSellerByUsername(token.getUsername());*/
        TbSeller seller = sellerJpaService.getSellerByUsername(token.getUsername());
        if (seller==null){
            return null;
        }
        if(!seller.getStatus().equals("1")){
            return null;
        }
        return new SimpleAuthenticationInfo(seller,seller.getPassword(), ByteSource.Util.bytes(seller.getSalt()),"");
    }
}
